package com.detection.yolo.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dahuatech.hutool.core.thread.ThreadUtil;
import com.dahuatech.icc.exception.ClientException;
import com.detection.common.config.DetectionConfig;
import com.detection.common.domain.hikvision.accessControlOnline.AccessControlPageResultView;
import com.detection.common.domain.hikvision.accessControlOnline.AccessControlView;
import com.detection.common.utils.CacheFileUtils;
import com.detection.dahua.model.accesscontrol.record.QueryRecordRequest;
import com.detection.dahua.model.accesscontrol.record.QueryRecordResponse;
import com.detection.dahua.model.brm.device.ChannelPageRequest;
import com.detection.dahua.model.brm.device.ChannelPageResponse;
import com.detection.dahua.model.brm.device.DeviceTreeRequest;
import com.detection.dahua.model.datasync.DataSync;
import com.detection.hikvision.config.CusArtemisConfig;
import com.detection.hikvision.domain.YqAccessControlEvent;
import com.detection.hikvision.service.AccessControlService;
import com.detection.hikvision.service.ArtemisCamerasService;
import com.detection.hikvision.utils.ArtemisApi;
import com.detection.hikvision.utils.HaiKangHttpUtils;
import com.detection.common.domain.YqEnvironmentDeviceInfo;
import com.detection.tcp.client.NettyTcpClient;
import com.detection.tcp.domain.TCPMessage;
import com.detection.tcp.utils.DockingUtils;
import com.detection.tcp.utils.ParseUtils;
import com.detection.common.domain.hikvision.accessControlStates.AccessControlState;
import com.detection.common.domain.hikvision.accessControlStates.AccessControlStateResultView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.detection.tcp.utils.DockingUtils.postForTCP;

@Component
@Slf4j
public class HikvisionDeviceTask {

    @Resource
    private CusArtemisConfig cusArtemisConfig;

    @Autowired
    private NettyTcpClient nettyTcpClient;

    @Autowired
    private AccessControlService accessControlService;

    @Autowired
    private ArtemisCamerasService artemisCamerasService;


    @Scheduled(cron = "0 0/5 * * * ?")
    public void sendEnvironmentDeviceInfo() throws InterruptedException {
        if (!"hikvision".equals(DetectionConfig.getIOTplatform())){
            return;
        }
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonBody = new JSONObject();
                jsonBody.put("pageNo", 1);
                jsonBody.put("pageSize", 999);
                HaiKangHttpUtils haiKangHttpUtils = new HaiKangHttpUtils();
//                String send = haiKangHttpUtils.send(cusArtemisConfig,jsonBody, ArtemisApi.GET_MONITOR_SEARCH);
                String send = "{\"code\":\"0\",\"msg\":\"success\",\"data\":{\"list\":[{\"indexCode\":\"9f4d56c89bb04f2eaf9c19662a1c3591\",\"name\":\"1号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"6c9939f0-005e-4f0a-bbc2-3c07c6c01c46\",\"parentIndexCode\":\"767b81097df142fcb298d83ca5a0b5ab\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"cd915c788e9b466fb944657c411a639a\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":10.8,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:43:10.190+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"9f4d56c89bb04f2eaf9c19662a1c3591\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"0178158f75044af280786caf2a399d74\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":38.299,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:43:08.200+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"9f4d56c89bb04f2eaf9c19662a1c3591\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"65503496bb834605abba2f4e792c1b98\",\"name\":\"1号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"6c9939f0-005e-4f0a-bbc2-3c07c6c01c46\",\"parentIndexCode\":\"ceefe7fec14741c084d3f68a32c85b39\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"f402a5e3d56f4215badc23f3781013c5\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"65503496bb834605abba2f4e792c1b98\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"35bb37adfafb46d2922995f60d18561f\",\"name\":\"2号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"db011a9d-1946-485b-adec-e8de092339a4\",\"parentIndexCode\":\"b45f738271cc49529d35633889d40b9b\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"ead00fb13b31436b89e826deef651550\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":-10.0,\"alarmHigh\":70.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":10.3,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:32.184+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"35bb37adfafb46d2922995f60d18561f\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"5af8cbb35a884dc39c44302f3cb33918\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":34.0,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:30.186+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"35bb37adfafb46d2922995f60d18561f\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"8666068a3c1a41d5a93e774aacd41f73\",\"name\":\"2号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"db011a9d-1946-485b-adec-e8de092339a4\",\"parentIndexCode\":\"ccb0e9fee60244e4b276def8c7047033\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"a0571f27f76b4af0995657cbf44d9674\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"8666068a3c1a41d5a93e774aacd41f73\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"0da862fe19544e2ebadac8a931c58a5d\",\"name\":\"3号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"5e17cf18-f670-47f4-9746-58b228881634\",\"parentIndexCode\":\"fa83f45835a740ca95a5f3db06c1bbe5\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"6631cd896387470db998cb25a33c1c46\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":10.8,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:59.190+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"0da862fe19544e2ebadac8a931c58a5d\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"a00dc2331d8641ec94b46cc185d3c249\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":51.799,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:43:01.187+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"0da862fe19544e2ebadac8a931c58a5d\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"f5dcab9bc5b04d6eb11ff30f883af96e\",\"name\":\"3号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"5e17cf18-f670-47f4-9746-58b228881634\",\"parentIndexCode\":\"878e15e7a3be4891a44a819b52738145\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"b7c03b05af4b4e899f66223e7e25ab79\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"f5dcab9bc5b04d6eb11ff30f883af96e\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"357556e6fc1e43aeadd209ab7d247366\",\"name\":\"4号库温湿度\",\"transducerType\":\"tempHum\",\"regionIndexCode\":\"937f1b47-d9d4-4619-a727-d6d665825f01\",\"parentIndexCode\":\"8619aaeab9b84d84a2841a4fe271ce6a\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"bd080f4da2ef42328e47db92f835555c\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":9.699,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:44.185+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"357556e6fc1e43aeadd209ab7d247366\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"36e669f39e544995897b652305c3d95e\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":62.799,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:43.184+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"357556e6fc1e43aeadd209ab7d247366\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"e3e820d9a37d46b9a388b14efb2fe6f5\",\"name\":\"4号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"937f1b47-d9d4-4619-a727-d6d665825f01\",\"parentIndexCode\":\"65d2c08f6187404faf25accf5457a3b6\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"3a337ea6a86b44beb466c3d30e8b111a\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"e3e820d9a37d46b9a388b14efb2fe6f5\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"01c065bd41d84aba97a9598b03776458\",\"name\":\"保障六队-13号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"372d9b15-6279-484e-bbf6-6565e20f6ed1\",\"parentIndexCode\":\"01c9f364eb5a4d9b856c2c6b96a2f7d1\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"439313cf08ce4255affd1327719b89af\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":9.0,\"latestIsAlarm\":true,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:42.185+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"01c065bd41d84aba97a9598b03776458\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":\"2\",\"stateName\":null},{\"indexCode\":\"0b936187fa944ff2aaa43a72465723fd\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":33.299,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:41.184+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"01c065bd41d84aba97a9598b03776458\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"6b2c97516d91431f841e075334824127\",\"name\":\"保障六队-13号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"372d9b15-6279-484e-bbf6-6565e20f6ed1\",\"parentIndexCode\":\"2d2b06d68d284aa7a4c103cd5341c6bc\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"2384a6e50d1b46c2b453e73cb56e7426\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"6b2c97516d91431f841e075334824127\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"734278d4277145079e129a9f6c264b8b\",\"name\":\"保障六队-14号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"9fc482b3-59d0-41d8-b45a-b09c159b5291\",\"parentIndexCode\":\"f0c7f1e7e00343ceb8fafdb9fabb2b2d\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"eb5e75ad525549d1b4612df67996816d\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":8.399,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:32.184+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"734278d4277145079e129a9f6c264b8b\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"66e2830c1b3b410586f680326059911b\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":43.2,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:31.184+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"734278d4277145079e129a9f6c264b8b\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"0a56b357e3c1441eb594fcee3e606932\",\"name\":\"保障六队-14号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"9fc482b3-59d0-41d8-b45a-b09c159b5291\",\"parentIndexCode\":\"57ab8ae378ed4b22a2d395bf7fc2d1d6\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"17293e90795c44dd8500a42dfbe978b1\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"0a56b357e3c1441eb594fcee3e606932\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"2be8b4840354438f9d77bc1f99581101\",\"name\":\"保障六队-15号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"842398b6-47d0-4dd7-9fa2-2355e6dd1bf6\",\"parentIndexCode\":\"1d9051f185be4435b166940619f515c7\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"4a420b36acf841849ac14d452cab7800\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":9.899,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:52.186+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"2be8b4840354438f9d77bc1f99581101\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"1d06118dccbc48269c66aab5539926e1\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":35.9,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:51.187+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"2be8b4840354438f9d77bc1f99581101\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"492e2fb543724389b68a5fd2bef65cc0\",\"name\":\"保障六队-15号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"842398b6-47d0-4dd7-9fa2-2355e6dd1bf6\",\"parentIndexCode\":\"8cf4e8d7208c4f479afb049687f421b5\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"ed8de52013e44a49848e51646cde67c7\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"492e2fb543724389b68a5fd2bef65cc0\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"3ad19344b3b74383a2f9ad631edbfbf4\",\"name\":\"保障六队-16号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"d6b4ad4c-34c5-4fe6-9646-5dc3b8f0b644\",\"parentIndexCode\":\"15b9fa77f4be4cd8b4ba2f1c842ace6c\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"338692d24681415ea51f8ebcc0af1afa\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":7.8,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:38.183+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"3ad19344b3b74383a2f9ad631edbfbf4\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"b554825c63ff4d26929a565accb08f12\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":41.799,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:42:40.186+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"3ad19344b3b74383a2f9ad631edbfbf4\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"0315f0d31b5348d3b9bad655e69737eb\",\"name\":\"保障六队-16号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"d6b4ad4c-34c5-4fe6-9646-5dc3b8f0b644\",\"parentIndexCode\":\"f10619b9b13b4eba93275c9858352a60\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"b53ba6236c6b4adc8b9a4fa635838beb\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"0315f0d31b5348d3b9bad655e69737eb\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"2a1179a221624c20b21e28bff1fe8cad\",\"name\":\"保障六队-17号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"4ce05836-7e87-42ab-9e91-dc98f8ad7eb9\",\"parentIndexCode\":\"7912ceb1c45644ddba47714de07024d3\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"d4c0253fdfe0404dbae674de8f881926\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":9.8,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:43:02.187+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"2a1179a221624c20b21e28bff1fe8cad\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"fa04aab0e0eb4cff9eccb5b1447ca9d2\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":31.899,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":\"2025-01-16T13:43:03.186+08:00\",\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"2a1179a221624c20b21e28bff1fe8cad\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"2c366b1f11e54855a2f59d58ba2df5c6\",\"name\":\"保障六队-17号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"4ce05836-7e87-42ab-9e91-dc98f8ad7eb9\",\"parentIndexCode\":\"dd97df4bc2cb4789be33d2a5340344ba\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"8c254857ca91468086d3166abb5e25b1\",\"name\":\"烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"2c366b1f11e54855a2f59d58ba2df5c6\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"209262e1905242e7904de3d3ac2a719a\",\"name\":\"保障六队-4号库温湿度\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"224adcf2-996c-4fef-bb45-b34a232d3c78\",\"parentIndexCode\":\"e5089ec3c89d4684a869c544d6f259a8\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"83a849edfb1f4137b733bd55fd825a62\",\"name\":\"传感器温度\",\"type\":\"temp\",\"unit\":\"℃\",\"channelNo\":null,\"alarmLow\":5.0,\"alarmHigh\":45.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"温度\",\"parentIndexCode\":\"209262e1905242e7904de3d3ac2a719a\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_temper.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null},{\"indexCode\":\"8a140612ba51424ea4bf130800ac9151\",\"name\":\"传感器湿度\",\"type\":\"humidity\",\"unit\":\"%\",\"channelNo\":null,\"alarmLow\":30.0,\"alarmHigh\":80.0,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":false,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"湿度\",\"parentIndexCode\":\"209262e1905242e7904de3d3ac2a719a\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":null,\"picUrl\":\"/pems/static/images/icon_humidity.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]},{\"indexCode\":\"27a0df70a24e44d4904fa313eaf4bf06\",\"name\":\"保障六队-4号库烟感\",\"transducerType\":\"generalTransducer\",\"regionIndexCode\":\"224adcf2-996c-4fef-bb45-b34a232d3c78\",\"parentIndexCode\":\"dafd282fc00044eaae7899ea9fdce498\",\"channelType\":\"channel_mock\",\"channelNo\":1,\"addressNo\":null,\"longitude\":null,\"latitude\":null,\"altitude\":null,\"sensorList\":[{\"indexCode\":\"14a3aaea102b489bb52192daa52a5651\",\"name\":\"4号库烟感\",\"type\":\"alarmPoint\",\"unit\":null,\"channelNo\":null,\"alarmLow\":null,\"alarmHigh\":null,\"measureLow\":null,\"measureHigh\":null,\"sensitive\":null,\"latestValue\":null,\"latestIsAlarm\":true,\"defenseFlag\":null,\"latestTime\":null,\"subSensorType\":null,\"isTopUp\":false,\"typeName\":\"告警点\",\"parentIndexCode\":\"27a0df70a24e44d4904fa313eaf4bf06\",\"parentName\":null,\"sensorShowValue\":null,\"controlStatus\":0,\"picUrl\":\"/pems/static/images/icon-alarming.png\",\"latestThresholdEventCode\":null,\"sensorThresholdName\":null,\"sensorThresholdType\":null,\"stateName\":null}]}],\"total\":30,\"pageNo\":1,\"pageSize\":20,\"totalPage\":null}}\n";
                List<YqEnvironmentDeviceInfo> res = parseJsonToDevices(send);
                TCPMessage message = new TCPMessage();
                message.setFlag("environmentDeviceInfo");
                message.setData(res);
                message.setCampCode(DetectionConfig.getCampCode());
                nettyTcpClient.sendMessage(ParseUtils.parse(message));
                postForTCP(message);
            }
        });

    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void sendAccessDeviceInfo() throws InterruptedException {
        if (!"hikvision".equals(DetectionConfig.getIOTplatform())){
            return;
        }
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                String hikvisionRootRegionId = DetectionConfig.getHikvisionRootRegionId();
                AccessControlPageResultView accessControlList = accessControlService.getAccessControlList(hikvisionRootRegionId, 1, 100);
                if (!ObjectUtils.isEmpty(accessControlList) && !ObjectUtils.isEmpty(accessControlList.getData()) && !CollectionUtils.isEmpty(accessControlList.getData().getList())) {
                    HashMap<String, String> accessControlAcsDoorList = accessControlService.getAccessControlAcsDoorList();
                    List<String> collect = accessControlList.getData().getList().stream().map(r -> accessControlAcsDoorList.get(r.getIndexCode())).collect(Collectors.toList());
                    AccessControlStateResultView accessControlStates = accessControlService.getAccessControlStates(collect.toArray(new String[]{}));
                    if (!ObjectUtils.isEmpty(accessControlStates) && !ObjectUtils.isEmpty(accessControlStates.getData()) && !CollectionUtils.isEmpty(accessControlStates.getData().getAuthDoorList())) {
                        for (AccessControlView accessControlView : accessControlList.getData().getList()) {
                            for (AccessControlState accessControlState : accessControlStates.getData().getAuthDoorList()) {
                                if (accessControlAcsDoorList.get(accessControlView.getIndexCode()) != null && accessControlAcsDoorList.get(accessControlView.getIndexCode()).equals(accessControlState.getDoorIndexCode())) {
                                    accessControlView.setDoorState(accessControlState.getDoorState());
                                    accessControlView.setDoorIndexCode(accessControlAcsDoorList.get(accessControlView.getIndexCode()));
                                }
                            }
                        }
                    }
                }
                List<AccessControlView> list = new ArrayList<>();
                for (AccessControlView accessControlView : accessControlList.getData().getList()) {
                    accessControlView.setCampCode(DetectionConfig.getCampCode());
                    accessControlView.setCampName(DetectionConfig.getCampName());
                    list.add(accessControlView);
                }
                int batchSize = 30;

                for (int i = 0; i < list.size(); i += batchSize) {
                    List<AccessControlView> subList = list.subList(i, Math.min(i + batchSize, list.size()));
                    TCPMessage message = new TCPMessage();
                    message.setFlag("SynchronizeAccseeList");
                    message.setData(JSON.toJSONString(subList));
                    message.setCampCode(DetectionConfig.getCampCode());
                    nettyTcpClient.sendMessage(ParseUtils.parse(message));
                    postForTCP(message);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

    }
    @Scheduled(cron = "0 0/5 * * * ?")
    public void sendCameraDeviceInfo() throws InterruptedException {
        if (!"hikvision".equals(DetectionConfig.getIOTplatform())){
            return;
        }
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                String result = artemisCamerasService.getCameraTreeList(1,1000);
                JSONObject resultObj = JSON.parseObject(result);
                JSONArray hkangArray = resultObj.getJSONArray("data");
                List<DataSync> dataSyncList = new ArrayList<>();
                List<String> indexCodeList = new ArrayList<>();
                List<String> indexLeafCodeList = new ArrayList<>();
                for(int i=0;i<hkangArray.size();i++){
                    JSONObject one = hkangArray.getJSONObject(i);
                    DataSync dataSync = new DataSync();
                    dataSync.setParentIndexCode(one.getString("parentIndexCode"));
                    dataSync.setName(one.getString("name"));
                    dataSync.setIndexCode(one.getString("indexCode"));
                    dataSync.setTreeCode(one.getIntValue("isRegion")==0?"1":"0");
                    dataSyncList.add(dataSync);
                    if("1".equals(dataSync.getTreeCode())){
                        indexLeafCodeList.add(dataSync.getIndexCode());
                    }
                    indexCodeList.add(dataSync.getIndexCode());

                }
//                Map<String, Integer> deviceMap = new HashMap<>(16);
//                deviceMap = getDeviceListByDeviceCode(indexLeafCodeList, deviceMap);

                for (DataSync oneDataSync : dataSyncList) {
//                    if ("1".equals(oneDataSync.getTreeCode())) {
//                        oneDataSync.setIsOnline(deviceMap.get(oneDataSync.getDeviceCode()));
                        oneDataSync.setIsOnline(1);
//                    }
                }
                int batchSize = 30;
                for (int i = 0; i < dataSyncList.size(); i += batchSize){
                    List<DataSync> subList = dataSyncList.subList(i, Math.min(i + batchSize, dataSyncList.size()));
                    TCPMessage message = new TCPMessage();
                    message.setId("11");
                    message.setFlag("SynchronizeVideoList");
                    message.setData(subList);
                    message.setCampCode(DetectionConfig.getCampCode());
                    nettyTcpClient.sendMessage(message);
                    postForTCP(message);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void updateAccessControlVariable() throws ClientException, InterruptedException {
        if (!"hikvision".equals(DetectionConfig.getIOTplatform())){
            return;
        }
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                Object startDateObj = CacheFileUtils.getCache("accessControl_startDate");
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
                ZonedDateTime nowWithZone = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
                ZonedDateTime fiveMinutesAgoWithZone = nowWithZone.minusMinutes(5);
                String endDateStr = nowWithZone.format(formatter); // 例如: 2025-10-14T16:30:00+08:00
                String startDateStr = fiveMinutesAgoWithZone.format(formatter);
                if(Objects.nonNull(startDateObj)){
                    startDateStr = startDateObj.toString();
                }
                String recordPage = artemisCamerasService.getDoorEvents(startDateStr,endDateStr,1,1000);
                JSONObject recordPageObj = JSON.parseObject(recordPage);
                JSONObject resultData = recordPageObj.getJSONObject("data");
                JSONArray resultList = resultData.getJSONArray("list");
//                List<YqAccessControlEvent> accessControlEventList = JSON.parseArray(resultList.toJSONString(), YqAccessControlEvent.class);

                int batchSize = 20;
                List<QueryRecordResponse.Data.PageData> sendList = new ArrayList<>();
                for (int i = 0; i < resultList.size(); i++ ){
                    JSONObject yqAccessControlEvent = resultList.getJSONObject(i);
                    QueryRecordResponse.Data.PageData pageData = new QueryRecordResponse.Data.PageData();

                    pageData.setSwingTime(yqAccessControlEvent.getString("eventTime"));
                    pageData.setPersonName(yqAccessControlEvent.getString("personName"));
//                    if(Objects.isNull(yqAccessControlEvent.getInOut())){
//                        yqAccessControlEvent.setInOut("0");
//                    }
                    pageData.setEnterOrExit(yqAccessControlEvent.getInteger("inAndOutType"));
                    pageData.setDeviceName(yqAccessControlEvent.getString("doorName"));

                    pageData.setOpenType(yqAccessControlEvent.getInteger("eventType"));
                    pageData.setPaperNumber(yqAccessControlEvent.getString("certNo"));
                    pageData.setHkPersonId(yqAccessControlEvent.getString("yqAccessControlEvent"));
                    sendList.add(pageData);
                    if(sendList.size()==20){
                        TCPMessage message = new TCPMessage();
                        message.setId("12");
                        message.setFlag("updateAccessControl");
                        message.setData(sendList);
                        message.setCampCode(DetectionConfig.getCampCode());
                        nettyTcpClient.sendMessage(message);
                        DockingUtils.postForTCP(message);
                        sendList = new ArrayList<>();
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }


                }
                if(sendList.size()>0){
                    TCPMessage message = new TCPMessage();
                    message.setId("12");
                    message.setFlag("updateAccessControl");
                    message.setData(sendList);
                    message.setCampCode(DetectionConfig.getCampCode());
                    nettyTcpClient.sendMessage(message);
                    DockingUtils.postForTCP(message);
                }
                CacheFileUtils.setCache("accessControl_startDate",endDateStr);

            }
        });

    }

    private Map<String, Integer> getDeviceListByDeviceCode(List<String> deviceCodeList, Map<String, Integer> deviceMap) {
        Integer size = deviceCodeList.size();
        if (size <= 400) {
            return getDeviceMap(size, deviceCodeList, deviceMap);
        } else {
            List<String> current = deviceCodeList.subList(0, 399);
            size = current.size();
            deviceMap = getDeviceMap(size, current, deviceMap);
            List<String> other = deviceCodeList.subList(400, deviceCodeList.size());
            return getDeviceListByDeviceCode(other, deviceMap);
        }
    }

    public Map<String, Integer> getDeviceMap(Integer size, List<String> deviceCodeList, Map<String, Integer> deviceMap) {
//        JSONArray array = artemisCamerasService.getCameraOnlineList(1,size,deviceCodeList);
//       for(int i=0;i<array.size();i++){
//           JSONObject one = array.getJSONObject(i);
//           deviceMap.put(one.getString("indexCode"),one.getIntValue("online"));
//       }
        for(String one : deviceCodeList){
            deviceMap.put(one,1);
        }
        return deviceMap;
    }

    public static List<YqEnvironmentDeviceInfo> parseJsonToDevices(String json) {
        List<YqEnvironmentDeviceInfo> resultList = new ArrayList<>();

        JSONObject jsonObject = JSON.parseObject(json);
        JSONArray list = jsonObject.getJSONObject("data").getJSONArray("list");

        for (int i = 0; i < list.size(); i++) {
            JSONObject deviceObj = list.getJSONObject(i);
            String deviceName = deviceObj.getString("name");
            String channelType = deviceObj.getString("channelType");
            String indexCode = deviceObj.getString("indexCode");

            JSONArray sensorList = deviceObj.getJSONArray("sensorList");
            boolean hasAlarm = sensorList.stream()
                    .map(s -> (JSONObject) s)
                    .anyMatch(s -> s.getBooleanValue("latestIsAlarm"));

            String status = hasAlarm ? "1" : "0";

            YqEnvironmentDeviceInfo.YqEnvironmentDeviceInfoBuilder builder = YqEnvironmentDeviceInfo.builder()
                    .deviceName(deviceName)
                    .deviceScene(DetectionConfig.getCampName())
                    .deviceModel(indexCode)
                    .ip("")
                    .collectionPeriod("5min")
                    .status(status);

            // 判断设备类型并设置读数
            if (deviceName.contains("温湿度") || isTempAndHumidityDevice(sensorList)) {
                builder.deviceType("tempHum");
                String temp = "N/A", humidity = "N/A";

                for (int j = 0; j < sensorList.size(); j++) {
                    JSONObject sensor = sensorList.getJSONObject(j);
                    String type = sensor.getString("type");
                    if ("temp".equals(type)) {
                        String latestValue = sensor.getString("latestValue");
                        if ("null".equals(latestValue) || StringUtils.isEmpty(latestValue)){
                            latestValue = "N/A";
                        }
                        temp = latestValue + " " + sensor.getString("unit");
                    } else if ("humidity".equals(type)) {
                        String latestValue = sensor.getString("latestValue");
                        if ("null".equals(latestValue) || StringUtils.isEmpty(latestValue)){
                            latestValue = "N/A";
                        }
                        humidity = latestValue + " " + sensor.getString("unit");
                    }
                }
                builder.reading1("温度: " + temp)
                        .reading2("湿度: " + humidity);

            } else if (deviceName.contains("烟感") || isAlarmPointDevice(sensorList)) {
                builder.deviceType("generalTransducer");
                String value = hasAlarm ? "NO" : "YES";
                builder.reading1(value)
                        .reading2(value); // 两个读数都为 YES/NO

            } else {
                builder.deviceType(deviceObj.getString("transducerType"))
                        .reading1("未知")
                        .reading2("");
            }

            resultList.add(builder.build());
        }

        return resultList;
    }

    // 判断是否为温湿度设备
    private static boolean isTempAndHumidityDevice(JSONArray sensorList) {
        boolean hasTemp = false, hasHumidity = false;
        for (int i = 0; i < sensorList.size(); i++) {
            String type = sensorList.getJSONObject(i).getString("type");
            if ("temp".equals(type)) hasTemp = true;
            if ("humidity".equals(type)) hasHumidity = true;
        }
        return hasTemp && hasHumidity;
    }

    // 判断是否为告警点设备（如烟感）
    private static boolean isAlarmPointDevice(JSONArray sensorList) {
        for (int i = 0; i < sensorList.size(); i++) {
            String type = sensorList.getJSONObject(i).getString("type");
            if ("alarmPoint".equals(type)) {
                return true;
            }
        }
        return false;
    }

}
